package com.dashu.base.utils;

import com.dashu.base.utils.encryption.DES;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

public class VerificationUtil {
    //获得当前请求绑定对象
//    private  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

    public static HttpServletRequest getRequest() {
        if (RequestContextHolder.getRequestAttributes() != null) {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        }
        return null;
    }

    public static String getRequestIp() {
        if (RequestContextHolder.getRequestAttributes() != null) {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteHost();
        }
        return null;
    }

    public static String getUserId() {
        String authorization = getRequest().getHeader("Authorization");
        String token = DES.decrypt(DES.DASHU_KEY, authorization);
        List<String> tokenList = Arrays.asList(token.split("-"));
        return tokenList.get(2);
    }
}
